package com.vdongchina.miniapp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.google.common.collect.Maps;
import com.vdongchina.miniapp.commons.MiniAppException;
import com.vdongchina.miniapp.configure.AppProperties;
import com.vdongchina.miniapp.http.dto.*;
import com.vdongchina.miniapp.http.service.IPayHttpService;
import com.vdongchina.miniapp.service.IAppPayService;
import com.vdongchina.miniapp.utils.PaymentSign;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import retrofit2.Call;

import java.util.Map;
import java.util.Objects;

/**
 * Copyright (C)
 * FileName: AppPayServiceImpl
 * Author:   maokai
 * Date:     2021/8/13 17:08
 * Description:
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class IAppPayServiceImpl implements IAppPayService {

    private final IPayHttpService payHttpService;
    private final AppProperties appProperties;

    /**
     * 支付下单
     */
    @Override
    public CreateOrderResp.Data createOrder(CreateOrderRequest request) throws MiniAppException {
        request.setSign(design(request));
        Call<CreateOrderResp> orderCall = payHttpService.createOrder(request);
        CreateOrderResp createOrderResp = payHttpService.parserResult(orderCall);
        if (Objects.isNull(createOrderResp)) {
            throw new MiniAppException("提交订单返回空");
        }
        createOrderResp.success();
        return createOrderResp.getData();
    }

    /**
     * 订单查询
     *
     * @param request
     */
    @Override
    public QueryOrderResp queryOrder(QueryOrderRequest request) throws MiniAppException {
        request.setSign(design(request));
        Call<QueryOrderResp> queryOrderRespCall = payHttpService.queryOrder(request);
        QueryOrderResp queryOrderResp = payHttpService.parserResult(queryOrderRespCall);
        if (Objects.isNull(queryOrderResp)) {
            throw new MiniAppException("查询订单返回空");
        }
        queryOrderResp.success();
        return queryOrderResp;
    }

    private String design(Object request) {
        Map<String, Object> data = BeanUtil.beanToMap(request, true, true);
        return PaymentSign.getSign(data, appProperties.getPayKey());
    }

    /**
     * 退款请求
     *
     * @param request
     */
    @Override
    public String createRefund(CreateRefundRequest request) throws MiniAppException {
        request.setSign(design(request));
        Call<CreateRefundResp> refund = payHttpService.createRefund(request);
        CreateRefundResp createRefundResp = payHttpService.parserResult(refund);
        if (Objects.isNull(createRefundResp)) {
            throw new MiniAppException("创建退款订单返回空");
        }
        createRefundResp.success();

        return createRefundResp.getRefundNo();
    }

    /**
     * 查询退款
     *
     * @param request
     */
    @Override
    public QueryRefundResp.RefundInfo queryRefund(QueryRefundRequest request) throws MiniAppException {
        request.setSign(design(request));
        Call<QueryRefundResp> queryRefundRespCall = payHttpService.queryRefund(request);
        QueryRefundResp queryRefundResp = payHttpService.parserResult(queryRefundRespCall);
        if (Objects.isNull(queryRefundResp)) {
            throw new MiniAppException("查询退款订单返回空");
        }
        queryRefundResp.success();
        return queryRefundResp.getRefundInfo();
    }

    /**
     * 分账
     *
     * @param request
     */
    @Override
    public boolean settle(SettleRequest request) throws MiniAppException {
        request.setSign(design(request));
        Call<SettleResp> settle = payHttpService.settle(request);
        SettleResp settleResp = payHttpService.parserResult(settle);
        if (Objects.isNull(settleResp)) {
            throw new MiniAppException("创建分账订单返回空");
        }
        settleResp.success();
        return true;
    }

    /**
     * 查询分账
     *
     * @param request
     */
    @Override
    public QuerySettleResp.SettleInfo querySettle(QuerySettleRequest request) throws MiniAppException {
        request.setSign(design(request));
        Call<QuerySettleResp> querySettleRespCall = payHttpService.querySettle(request);
        QuerySettleResp querySettleResp = payHttpService.parserResult(querySettleRespCall);
        if (Objects.isNull(querySettleResp)) {
            throw new MiniAppException("查询分账订单返回空");
        }
        querySettleResp.success();
        return querySettleResp.getSettleInfo();
    }

    /**
     * 担保交易账单查询
     *
     * @param startDate 开始时间，格式：20210603
     * @param endDate   结束时间，格式：20210603
     * @param seller    商户号
     * @param billType  账单类型，包括 payment:支付账单, settle:分账账单, refund:退款账单
     * @param appId     和商户号绑定的 appid 或者 thirdparty_id
     * @param sign      对上面的参数进行签名
     */
    @Override
    public String bill(String startDate, String endDate, String seller, String billType, String appId) throws MiniAppException {
        Map<String, Object> data = Maps.newHashMap();
        data.put("start_date", startDate);
        data.put("end_date", endDate);
        data.put("seller", seller);
        data.put("bill_type", billType);
        data.put("app_id", appId);
        String payKey = appProperties.getPayKey();
        String sign = PaymentSign.getSign(data, payKey);
        Call<BillResp> billCall = payHttpService.bill(startDate, endDate, seller, billType, appId, sign);
        BillResp billResp = payHttpService.parserResult(billCall);
        if (Objects.isNull(billResp)) {
            throw new MiniAppException("担保交易账单查询返回空");
        }
        billResp.success();
        return billResp.getData();
    }

}
